import { contextBridge } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
export const API = {
  handlePreloadApiOpenDirectoryDialog: () =>
    electronAPI.ipcRenderer.invoke('ipcPreloadMainOpenFolderGetPath') as Promise<
      (GitBackInfo & GitBackInfoError) | undefined
    >,
  handlePreloadApiChangeGitInfos: (params: GitChangeParams) =>
    electronAPI.ipcRenderer.invoke('ipcPreloadMainChangeGitInfos', params) as Promise<
      (GitBackInfo & GitBackInfoError) | undefined
    >
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', API)
  } catch (error) {
    console.error(error)
  }
} else {
  // @ts-ignore (define in dts)
  window.electron = electronAPI
  // @ts-ignore (define in dts)
  window.api = api
}
